// tesst1------
const promise = new Promise((resolve, reject) => {
    console.log(1);
    console.log(2);
    // 此处没有调用 resolve（），所以触发不了.then()
});
promise.then(() => {
    console.log(3);
});
console.log(4);
// 结果为 1,2,4 


// test2-------
setTimeout(function () {
    console.log(1);
}, 100); // 延迟100毫秒

new Promise(function (resolve) {
    console.log(2);
    resolve();
    console.log(3);
}).then(function () {
    console.log(4);
    new Promise((resove, reject) => {
        console.log(5);
        setTimeout(() => {
            console.log(6);
        }, 10); // 延迟10毫秒
    })
});
console.log(7);
console.log(8);

// 结果：2,3,7,8,4,5,6,1
// 如果两个setTimeout()函数的delay参数相同，那么它们会按照代码的顺序依次执行。 先1后6


// test3------
function foo() {
    console.log('foo')
    console.log(this)
    console.log(this.a);
}

function doFoo() {
    console.log('dofoo')
    console.log(this)
    foo()
    // foo.call(this);  // 结果：dofoo，{a:1，doFoo：f}，foo，{a:1，doFoo：f}，1
}

var obj = {
    a: 1,
    doFoo: doFoo
};

var a = 2;
obj.doFoo()

// 结果：dofoo，{a:1，doFoo：f}，foo，window，2




// myTest1-----
async function foo() {
    console.log('foo')
}
async function bar() {
    console.log('bar start')
    await foo()
    console.log('bar end')
}
console.log('script start')
setTimeout(function () {
    console.log('setTimeout')
}, 0)
bar();
new Promise(function (resolve) {
    console.log('promise executor')
    resolve();
}).then(function () {
    console.log('promise then')
})
console.log('script end')

// 结果：script start, bar start, foo, promise executor, script end, bar end, promise then, setTimeout


// async await  隐式返回一个promise

// myTest1----
async function foo() {
    return 2
}
console.log(foo())  // Promise {<fulfilled>: 2}


// myTest4----
async function foo() {
    console.log(1)
    let a = await 100
    console.log(a)
    console.log(2)
}
console.log(0)
foo()
console.log(3)

// 结果：0,1,3,100,2

// myTest5----
function s() {
    console.log(4)
    return 100
}

async function foo() {
    console.log(1)
    let a = await s()
    console.log(a)
    console.log(2)
}
console.log(0)
foo()
console.log(3)

// 结果：0,1,4,3,100,2